package com.hjy.web;

import cn.hutool.core.io.IoUtil;
import com.hjy.common.utils.R;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.Charset;

@RestController
@RequestMapping("/test")
public class TestController {

    @RequestMapping("/1")
    public R test1(HttpServletRequest request) throws IOException {
        getMessage(request);
        return R.ok();
    }

    @RequestMapping("/2")
    public R test2(@RequestBody User user, HttpServletRequest request) throws IOException {
        System.out.println("user = " + user);
        getMessage(request);
        return R.ok();
    }

    @RequestMapping(value = "/3", method = RequestMethod.POST)
    public R test3(User user, HttpServletRequest request) throws IOException {
        System.out.println("user = " + user);
        getMessage(request);
        return R.ok();
    }

    @RequestMapping("/4")
    public R test4(@RequestBody User user, HttpServletRequest request) throws IOException {
        System.out.println("user = " + user);
        getMessage(request);
        return R.ok();
    }

    @RequestMapping(value = "/5", method = RequestMethod.POST)
    public R test5(@RequestBody User user, HttpServletRequest request) throws IOException {
        System.out.println("user = " + user);
        getMessage(request);
        return R.ok();
    }

    @RequestMapping(value = "/6")
    public R test6(@RequestBody User user ,String hello, HttpServletRequest request) throws IOException {
        System.out.println("user = " + user);
        System.out.println("hello = " + hello);
        getMessage(request);
        return R.ok();
    }

    @RequestMapping(value = "/7", method = RequestMethod.POST)
    public R test7(@RequestBody User user, @RequestParam("hello") String hello, HttpServletRequest request) throws IOException {
        System.out.println("user = " + user);
        System.out.println("hello = " + hello);
        getMessage(request);
        return R.ok();
    }


    @RequestMapping(value = "/8")
    public R test8(String hello1, @RequestParam("hello2") String hello2, HttpServletRequest request) throws IOException {
        System.out.println("hello1 = " + hello1);
        System.out.println("hello2 = " + hello2);
        getMessage(request);
        return R.ok();
    }

    @RequestMapping(value = "/9")
    public R test9(@RequestParam("hello1") String hello1, @RequestParam("hello2") String hello2, HttpServletRequest request) throws IOException {
        System.out.println("hello1 = " + hello1);
        System.out.println("hello2 = " + hello2);
        getMessage(request);
        return R.ok();
    }

    @RequestMapping(value = "/t/{id}")
    public R test10(@PathVariable("id") String id, HttpServletRequest request) throws IOException {
        System.out.println("id = " + id);
        getMessage(request);
        return R.ok();
    }

    @Data
    public static class User {
        private String username;
        private String password;
    }

    public void getMessage(HttpServletRequest request) throws IOException {
        String method = request.getMethod();
        System.out.println("method = " + method);
        String remoteHost = request.getRemoteHost();
        System.out.println("remoteHost = " + remoteHost);
        ServletInputStream inputStream = request.getInputStream();
        String read = IoUtil.read(inputStream, Charset.defaultCharset());
        System.out.println("read = " + read);
    }
}
